package cn.mall.member.controller;

import cn.mall.common.constant.MemberResponseVo;
import cn.mall.common.utils.PageUtils;
import cn.mall.common.utils.R;
import cn.mall.member.VO.RegistVO;
import cn.mall.member.VO.UserLoginVo;
import cn.mall.common.vo.MemberEntity;
import cn.mall.member.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.Arrays;
import java.util.Map;
import cn.mall.common.exception.BizCodeEnum;



/**
 * 会员
 *
 * @author ssd
 * @email sunlightcs@gmail.com
 * @date 2023-10-19 11:00:17
 */
@RestController
@RequestMapping("member/member")
public class MemberController {
    @Autowired
    private MemberService memberService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = memberService.queryPage(params);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
        System.out.println("id = " + id);
		MemberEntity member = memberService.getById(id);
        return R.ok().put("member", member);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody MemberEntity member){
		memberService.save(member);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody MemberEntity member){
		memberService.updateById(member);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
		memberService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }
    /**
     * 会员登录
     * @param
     * @return
     */
    @PostMapping("/login")
    public R login(@RequestBody UserLoginVo loginVo){
        MemberResponseVo login = memberService.login(loginVo);
        if (login == null) {
            return R.error(BizCodeEnum.LOGINACCT_PASSWORD_EXCEPTION.getCode(),
                    BizCodeEnum.LOGINACCT_PASSWORD_EXCEPTION.getMessage());
        }
        return R.ok().put("data", login);

    }
    @PostMapping("/reg/select")
    public  R registAndSelect(@RequestBody RegistVO registVO){
        R r=memberService.selectAndRegist(registVO.getName(),registVO.getTel());
        return  r;

    }
}
